package edu.whu.tao.controller;

import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;

import com.xiaoleilu.hutool.log.Log;
import com.xiaoleilu.hutool.log.LogFactory;

import edu.whu.tao.util.CommonResult;
import edu.whu.tao.util.DateEditor;
import edu.whu.tao.util.WebUtils;

/**
 * 
 * <P>TODO</P>
 * @author 陶焕(13294175866)
 * @date 2016年4月27日 下午8:33:08
 */
public class BaseController {

	private final static Log log = LogFactory.get();
	
	private static final String ERROR_MSG = "系统内部异常";
	
	/*@InitBinder
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
		binder.registerCustomEditor(Date.class, new DateEditor());
	}*/

	@ExceptionHandler(Exception.class)
	public Object handleAllException(HttpServletRequest request, HttpServletResponse response, Exception e) {
		e.printStackTrace();
		log.error("BaseController.handleAllException:{}", e);// log4j打印错误级别堆栈日志信息，时间
		return handleError(request, response, e);
	}
	
	protected Object handleError(HttpServletRequest request, HttpServletResponse response, Exception e) {
		CommonResult<String> result = new CommonResult<String>(CommonResult.failureCode);
		result.setMessage(ERROR_MSG);
		WebUtils.returnJson(response, result);
		return null;
	}
	
}
